package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.LvhuaDao;
import com.hdaccp.xingmu.entity.Lvhua;
import com.hdaccp.xingmu.entity.Patrol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/lvhua")
public class LvhuaCon {


    @Autowired
    private LvhuaDao lvhuaDao;

    @PersistenceContext
    private EntityManager entityManager ;

    @RequestMapping("Qtiaozhuan")
    public String Qtiaozhuan(){
        return "WEB-INF/jsp/shequfuwu/baojielvhua";
    }


    //查询全部
    @RequestMapping("/quanbujianyi")
    @ResponseBody
    public Map<String ,Object> quanbujinayi(String firstDate,String secondDate,String type,int pageSize, int pageNumber) {
        //分页
        String jpal="SELECT p.id,p.order,p.address,i.name as iname,p.name,p.date,p.ys,p.remark FROM Lvhua AS p \n" +
                "INNER JOIN Lvhuatype AS i ON p.type = i.id WHERE 1=1";

        if (type != null && !type.equals("")) {
            jpal = jpal + " and type=" + "'" + type + "' ";
        }else if (firstDate!=null && !firstDate.equals("") && secondDate!=null && !secondDate.equals("") ){
            jpal=jpal+" and date>=" +"'" + secondDate +"' and date<=" +"'" + firstDate +"' ";
        }

        Query qu = entityManager.createQuery(jpal);

        //起始页书
        qu.setFirstResult((pageNumber - 1) * pageSize);
        //结束页数
        qu.setMaxResults(pageSize);

        //查询多少条数据
        String jpa = "select count(m) from Lvhua m where 1=1";
        if (type != null && !type.equals("")) {
            jpa = jpa + " and type=" + "'" + type + "' ";
        }else if (firstDate!=null && !firstDate.equals("") && secondDate!=null && !secondDate.equals("") ){
            jpa=jpa+" and date>=" +"'" + secondDate +"' and date<=" +"'" + firstDate +"' ";
        }

        Query q = entityManager.createQuery(jpa);

        Long count = (Long) q.getSingleResult();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", qu.getResultList());
        return map;
    }

    //删除数据
    @RequestMapping("/Qdel")
    @ResponseBody
    public Map<String ,Object> Qdel(long id,int pageSize,int pageNumber){

        lvhuaDao.deleteById(id);


        return quanbujinayi(null,null,null,pageSize,pageNumber);
    }


    //添加信息
    @RequestMapping("/Qtianjia")
    @ResponseBody
    public Map<String ,Object> add2(Lvhua lvhua,int pageSize,int pageNumber){
        Lvhua lvhua1=new Lvhua();
        lvhua1.setOrder(lvhua.getOrder());
        lvhua1.setType(lvhua.getType());
        lvhua1.setAddress(lvhua.getAddress());
        lvhua1.setName(lvhua.getName());
        lvhua1.setDate(lvhua.getDate());
        lvhua1.setYs(lvhua.getYs());
        lvhua1.setRemark(lvhua.getRemark());
        lvhuaDao.save(lvhua1);
        return quanbujinayi(null,null,null,pageSize,pageNumber);
    }
    //修改数据
    @RequestMapping("Qupd")
    @ResponseBody
    public Map<String ,Object> Qupd(int id,String order,int type,String address,String name,String date ,String ys,String remark,int pageSize,int pageNumber){

        Lvhua lvhua=new Lvhua();
        lvhua.setId(id);
        lvhua.setOrder(order);
        lvhua.setName(name);
        lvhua.setType(type);
        lvhua.setAddress(address);
        lvhua.setDate(Date.valueOf(date));
        lvhua.setYs(ys);
        lvhua.setRemark(remark);
        Lvhua advice2 = lvhuaDao.save(lvhua);
        return quanbujinayi(null,null,null,pageSize,pageNumber);
    }



}
